local skel = fk.CreateSkill {
  name = "emo__jielei",
}

Fk:loadTranslationTable{
  ["emo__jielei"] = "解缧",
  [":emo__jielei"] = "每回合各一次，你可以将一张牌当【铁索连环】或雷【杀】使用，结算后若此牌恰使一名角色脱离横置，其须执行以下一项，然后你可以视为执行另一项：1.使用一张单体伤害牌；2.重铸一张牌。",

  ["#emo__jielei"] = "解缧:将一张牌当【铁索连环】或雷【杀】使用",
  ["#emo__jielei-use"] = "解缧：请使用1张单体伤害牌，否则重铸1张牌",
  ["#emo__jielei-draw"] = "解缧：你可以视为重铸1张牌",
  ["#emo__jielei-recast"] = "解缧：你须重铸1张牌！",
  ["#emo__jielei-vs"] = "解缧：你可以视为使用一张单体伤害牌",

  ["$emo__jielei1"] = "",
  ["$emo__jielei2"] = "",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  prompt = "#emo__jielei",
  pattern = "slash,iron_chain",
  interaction = function(self, player)
    local all_choices = table.filter({"iron_chain", "thunder__slash"}, function (name)
      return not table.contains(player:getTableMark("emo__jielei-turn"), name)
    end)
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function(self, player, use)
    player.room:addTableMark(player, "emo__jielei-turn", use.card.name)
  end,
  after_use = function (self, player, use)
    local room = player.room
    if use.extra_data and use.extra_data[skel.name] then
      local targets = use.extra_data[skel.name]
      if #targets ~= 1 then return end
      local to = targets[1]---@type ServerPlayer
      if to.dead then return end
      local names, tnames = {}, {}
      local extra_data = {bypass_times = true}
      for _, name in ipairs(Fk.all_card_names) do
        local card = Fk:cloneCard(name)
        card:setVSPattern(nil, to, ".")
        if not to:prohibitUse(card) and to:canUse(card, extra_data) then
          if card.is_damage_card and not card.is_passive and not card.multiple_targets then
            table.insert(names, card.name)
            table.insertIfNeed(tnames, card.trueName)
          end
        end
      end
      local pattern = table.concat(tnames, ",") .. "|.|.|.|" .. table.concat(names, ",")
      local ask_use = room:askToUseCard(to, {
        skill_name = skel.name,
        pattern = pattern,
        prompt = "#emo__jielei-use",
        cancelable = true,
        extra_data = extra_data,
      })
      if ask_use then
        ask_use.extraUse = true
        room:useCard(ask_use)
        if not player.dead and room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__jielei-draw"}) then
          room:broadcastPlaySound("./audio/system/recast")
          player:drawCards(1, skel.name)
        end
      elseif not to:isNude() then -- 重铸
        local cards = room:askToCards(to, {
          min_num = 1, max_num = 1, cancelable = false, include_equip = true,
          prompt = "#emo__jielei-recast", skill_name = skel.name,
        })
        room:recastCard(cards, to, skel.name)
        if not player.dead then
          names = table.filter(Fk:getAllCardNames("bt"), function (name)
            local card = Fk:cloneCard(name)
            return card.is_damage_card and not card.is_passive and not card.multiple_targets
          end)
          room:askToUseVirtualCard(player, {
            skill_name = skel.name, name = names, prompt = "#emo__jielei-vs",
          })
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return table.find({"iron_chain", "thunder__slash"}, function (name)
      return not table.contains(player:getTableMark("emo__jielei-turn"), name)
    end) ~= nil
  end,
  enabled_at_response = function (self, player, response)
    if not response then
      local all_choices = table.filter({"iron_chain", "thunder__slash"}, function (name)
        return not table.contains(player:getTableMark("emo__jielei-turn"), name)
      end)
      return #player:getViewAsCardNames(skel.name, all_choices) > 0
    end
  end,
})

skel:addEffect(fk.ChainStateChanged, {
  can_refresh = function (self, event, target, player, data)
    return target == player and not player.chained
  end,
  on_refresh = function (self, event, target, player, data)
    local effect_event = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect, true)
    if effect_event == nil then return end
    local use_event = effect_event:findParent(GameEvent.UseCard)
    if use_event == nil then return end
    local use = use_event.data
    if table.contains(use.card.skillNames, skel.name) then
      use.extra_data = use.extra_data or {}
      use.extra_data[skel.name]  = use.extra_data[skel.name] or {}
      table.insertIfNeed(use.extra_data[skel.name], target)
    end
  end,
})

return skel
